package com.cwg.dict.ui.rcp.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

import com.cwg.dict.stem.model.StemType;
import com.cwg.dict.stem.model.Token;

public class StemView extends ViewPart {
	public final static String	ID	= "com.cwg.dict.StemView";
	private Composite			parent;
	private Token[]				tokens;

	@Override
	public void createPartControl(Composite parent) {
		//parent.setLayout(new FillLayout());
		this.parent = parent;
		setTokens(new Token[0]);
	}

	public Token[] getTokens() {
		return tokens;
	}

	@Override
	public void setFocus() {
	}

	public void setTokens(Token[] tokens) {
		this.tokens = tokens;
		Control[] children = parent.getChildren();
		// clear all children
		for (Control each : children) {
			if (each != null) {
				each.dispose();
			}
		}
		FillLayout layout = new FillLayout();
		parent.setLayout(layout);
		// recreate new children
		Composite c = new Composite(parent, parent.getStyle());
		c.setLayout(layout);

		ExpandBar bar = new ExpandBar(c, SWT.V_SCROLL);
		createExpandItem(bar, tokens, StemType.SUFFIX);
		createExpandItem(bar, tokens, StemType.STEM);
		createExpandItem(bar, tokens, StemType.PREFIX);
		bar.setSpacing(5);
		/*
		 *  TODO: find a method to force a event that can enforce repaint in Composite c
		 *  this size don't fit to the client, but I have no idea how to layout the widget prefactly here. 
		 */
		c.setSize(1000, 1000);

	}

	private void createExpandItem(ExpandBar bar, Token[] query, StemType stemType) {
		// First item
		Composite composite = new Composite(bar, SWT.NONE);
		GridLayout layout = new GridLayout(2, true);
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		composite.setLayout(layout);
		int count = 0;
		for (Token each : query) {
			if (stemType != each.getGroup().getSection().getCategory()) {
				continue;
			}
			count++;
			Group g = new Group(composite, SWT.SHADOW_ETCHED_IN);
			g.setLayout(new GridLayout(1, true));
			createPair(g, "name", each.getName());
			if (StemType.PREFIX == each.getGroup().getSection().getCategory() || StemType.STEM == each.getGroup().getSection().getCategory()) {
				createPair(g, "orgnal", each.getOriginal());
				createPair(g, "translate", each.getTranslate());
				createPair(g, "description", each.getDescription());
			} else if (StemType.SUFFIX == each.getGroup().getSection().getCategory()) {
				createPair(g, "orgnal", each.getGroup().getSection().getDescription());
			}
		}

		ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
		item.setText(stemType.toString() + " (" + count + ")");
		item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item.setControl(composite);
		item.setImage(Display.getDefault().getSystemImage(SWT.ICON_QUESTION));
		item.setExpanded(count > 0);

	}

	private void createPair(Composite parent, String caption, String text) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(caption + " :");
		Label label2 = new Label(parent, SWT.NONE);
		if (text != null) {
			label2.setText(text);
		}
	}

}
